//index.js
//获取应用实例
var http = require("../../utils/http.js");
var config = require("../../utils/config.js");
// const { get } = require("http");
const app = getApp()

Page({
  data: {
    dropDownMenuTitle: ['排序', '销量', '速度', '配送费'],
    dropDownMenuFirstData: [{
      id: 1,
      title: '智能排序'
    }, {
      id: 2,
      title: '评分优先'
    }, {
      id: 3,
      title: '距离优先'
    }], //排序数据
    dropDownMenuSecondData: [{
        id: 'desc',
        title: '高到低'
      },
      {
        id: 2,
        title: '低到高'
      }
    ],
    dropDownMenuThirdData: [{
        id: 1,
        title: '升序'
      },
      {
        id: 2,
        title: '降序'
      }
    ],
    dropDownMenuFourthData: [{
        id: 1,
        title: '升序'
      },
      {
        id: 2,
        title: '降序'
      }
    ],
    indicatorDots: true,
    indicatorColor: '#d1e5fb',
    indicatorActiveColor: '#1b7dec',
    autoplay: true,
    interval: 2000,
    duration: 1000,
    indexImgs: [],
    seq: 0,
    news: [],
    merchantList: [],
    sts: 0,
    city: app.globalData.city
  },
  //选择城市
  selectCity: function () {
    wx.navigateTo({
      url: '../../pages/selectCity/selectCity'
    })
  },
  selectedFourth: function (e) {
    var ths = this;
    console.log("选中第" + e.detail.index + "个标签，选中的id：" + e.detail.selectedId + "；选中的内容：" + e.detail.selectedTitle);
    if (e.detail.index == 1 && e.detail.selectedId == 1) {} else if (e.detail.index == 1 && e.detail.selectedId == 2) {
      ths.getMerchantByScore();
    } else if (e.detail.index == 1 && e.detail.selectedId == 3) {
      ths.getMerchantSortByDistance();
    } else if (e.detail.index == 2 && e.detail.selectedId == 'desc') {
      ths.getMerchantBySalesDown();
    } else if (e.detail.index == 2 && e.detail.selectedId == 2) {
      ths.getMerchantBySalesUp();
    } else if (e.detail.index == 3 && e.detail.selectedId == 1) {
      ths.getMerchantBySpeedDown();
    } else if (e.detail.index == 3 && e.detail.selectedId == 2) {
      ths.getMerchantBySpeedUp();
    } else if (e.detail.index == 4 && e.detail.selectedId == 1) {
      ths.getMerchantByReachPriceDown();
    } else if (e.detail.index == 4 && e.detail.selectedId == 2) {
      ths.getMerchantByReachPriceUp();
    }
  },
  showDialog: function (e) {

  },
  //取消事件
  _cancelEvent: function (e) {
    console.log('你点击了取消');
    this.dialog.hideDialog();
  },
  //确认事件
  _confirmEvent: function (e) {
    console.log('你点击了确定');
    this.dialog.hideDialog();
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var that = this;
    this.setData({
      city: app.globalData.city,
    });
    this.getAllData();

  },

  // 页面滚动到指定位置指定元素固定在顶部
  onPageScroll: function (e) { //监听页面滚动
    this.setData({
      scrollTop: e.scrollTop
    })
  },

  toProdPage: function (e) {
    var prodid = e.currentTarget.dataset.prodid;
    if (prodid) {
      wx.navigateTo({
        url: '/pages/prod/prod?prodid=' + prodid,
      })
    }
  },

  toCouponCenter: function () {
    wx.showToast({
      icon: "none",
      title: '该功能未开源'
    })
  },

  // 跳转搜索页
  toSearchPage: function () {
    wx.navigateTo({
      url: '/pages/search-page/search-page',
    })
  },

  //跳转商品活动页面
  toClassifyPage: function (e) {
    var url = '/pages/prod-classify/prod-classify?sts=' + e.currentTarget.dataset.sts;
    // var id = e.currentTarget.dataset.id;toCouponCenter
    var title = e.currentTarget.dataset.title;
    // if (id) {
    //   url += "&tagid=" + id + "&title=" + title;
    // }
    wx.navigateTo({
      url: url
    })
  },
  //
  showMap() {
    wx.navigateTo({
      url: '/pages/map/map?city=' + this.data.city,
    })
  },
  //跳转公告列表页面
  onNewsPage: function () {
    wx.navigateTo({
      url: '/pages/recent-news/recent-news',
    })
  },

  onShow: function () {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.navigateTo({
            url: '/pages/login/login',
          })
        }
      }
    })


  },
  getAllData() {
    http.getCartCount(); //重新计算购物车总数量
    this.getIndexImgs();
    // this.getNoticeList();
    this.getMerchant();
  },
  //加载轮播图
  getIndexImgs() {
    //加载轮播图
    var params = {
      url: "/platform/indexImage/getAll",
      method: "GET",
      data: {},
      callBack: (res) => {
        this.setData({
          indexImgs: res.content,
          seq: res
        });
      }
    };
    http.request(params);
  },
  getNoticeList() {
    // 加载公告
    var params = {
      url: "/platform/notice/topNoticeList",
      method: "GET",
      data: {},
      callBack: (res) => {
        this.setData({
          news: res,
        });
      }
    };
    http.request(params);
  },


  // 加载商品标题分组列表
  getMerchant() {
    let ths=this
    wx.getLocation({
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度
      success(res) {
        var params = {
          url: "/platform/merchant/getByCity",
          method: "GET",
          data: {
            city: app.globalData.city,
            latitude: res.latitude,
            longitude: res.longitude
          },
          callBack: (res) => {
            ths.setData({
              merchantList: res.content,
            });
          }
        };
        http.request(params);
      }
    })
  },

  getTagProd(id, index) {
    var param = {
      url: "/prod/prodListByTagId",
      method: "GET",
      data: {
        tagId: id,
        size: 6
      },
      callBack: (res) => {
        var taglist = this.data.taglist;
        taglist[index].prods = res.records

        this.setData({
          taglist: taglist,
        });
      }
    };
    http.request(param);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  // onPullDownRefresh: function () {
  //     wx.request({
  //       url: '',
  //       data: {},
  //       method: 'GET',
  //       success: function (res) { },
  //       fail: function (res) { },
  //       complete: function (res) {
  //         wx.stopPullDownRefresh();
  //       }
  //     })
  // },

  onPullDownRefresh: function () {

    // wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    var ths = this;
    setTimeout(function () {

      ths.getAllData();

      // wx.hideNavigationBarLoading() //完成停止加载

      wx.stopPullDownRefresh() //停止下拉刷新

    }, 100);

  },

  /**
   * 商户评分排序
   */
  getMerchantByScore() {
    var ths = this;
    var params = {
      url: "/platform/merchant/getMerchantSortByScore",
      method: "GET",
      data: {},
      callBack: function (res) {
        console.log(app.globalData.city)
        ths.setData({
          merchantList: res.content,
        });
      }
    };
    http.request(params);
  },

  /**
   * 商户距离排序
   */
  getMerchantSortByDistance() {
    var ths = this;
    wx.getLocation({
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度
      success(res) {
        console.log(res)
        const latitude = res.latitude
        const longitude = res.longitude
        var params = {
          url: "/platform/merchant/getMerchantSortByDistance",
          method: "GET",
          data: {
            latitude: latitude, //纬度
            longitude: longitude //经度
          },
          callBack: function (res) {
            ths.setData({
              merchantList: res.content,
            });
          }
        };
        http.request(params);
      }
    })
  },

  /**
   * 根据用户的销量排序
   */
  getMerchantBySalesUp() {
    var ths = this;
    var params = {
      url: "/platform/merchant/getMerchantBySalesUp",
      method: "GET",
      data: {
        // asc: asc,
        // desc: desc
      },
      callBack: function (res) {
        ths.setData({
          merchantList: res.content,
        });
      }
    };
    http.request(params);
  },

  getMerchantBySalesDown() {
    var ths = this;
    var params = {
      url: "/platform/merchant/getMerchantBySalesDown",
      method: "GET",
      data: {
        // asc: asc,
        // desc: desc
      },
      callBack: function (res) {
        ths.setData({
          merchantList: res.content,
        });
      }
    };
    http.request(params);
  },

  getMerchantBySpeedDown() {
    var ths = this;
    var params = {
      url: "/platform/merchant/getMerchantBySpeedDown",
      method: "GET",
      callBack: function (res) {
        ths.setData({
          merchantList: res.content,
        });
      }
    };
    http.request(params);
  },

  getMerchantBySpeedUp() {
    var ths = this;
    var params = {
      url: "/platform/merchant/getMerchantBySpeedUp",
      method: "GET",
      callBack: function (res) {
        ths.setData({
          merchantList: res.content,
        });
      }
    };
    http.request(params);
  },

  getMerchantByReachPriceUp() {
    var ths = this;
    var params = {
      url: "/platform/merchant/getMerchantByReachPriceUp",
      method: "GET",
      callBack: function (res) {
        ths.setData({
          merchantList: res.content,
        });
      }
    };
    http.request(params);
  },

  getMerchantByReachPriceDown() {
    var ths = this;
    var params = {
      url: "/platform/merchant/getMerchantByReachPriceDown",
      method: "GET",
      callBack: function (res) {
        ths.setData({
          merchantList: res.content,
        });
      }
    };
    http.request(params);
  },


  /**
   * 跳转至商家页面
   */
  gotoMerchant: function (e) {
    var merchantId = e.currentTarget.dataset.id;
    var distance= e.currentTarget.dataset.distance;
    if (merchantId) {
      wx.navigateTo({
        url: '/pages/category/category?merchantId=' + merchantId+'&distance='+distance,
      })
    }
  }
})